/**
 * @author trex279
 */

//@private
var submissionsFileName = "submissions.json";
var newSubmissions = {};
var oldSubmissions = {};
var oldSubmissionsArray = new Array();
var newSubmissionsArray = new Array();
var now = new Date(); //time now
var noNewComments = 0;

function getsubmissions(){
	$.getJSON("http://www.reddit.com/user/"+userName_g+"/submitted/.json",
    	function(redditJson){
			if (redditJson == "") {
				thisApplication.nativeWin.dispatchEvent(loadingErrorEvent);
				return;
			}
			
			newSubmissions = redditJson;
			newSubmissionsArray = redditJson.data.children;
			
			var submissionsJsonFromFile = readJsonFromFile(submissionsFileName);
			if(submissionsJsonFromFile==-1){
				//alert("File size 0");
				storeJson(redditJson,submissionsFileName);
				submissionsJsonFromFile = readJsonFromFile(submissionsFileName);
			}
			 
			var oldSubmissionsJson = $.evalJSON(submissionsJsonFromFile);
			oldSubmissionsArray = oldSubmissionsJson.data.children;
			
			processsubmissions();
			displaysubmissions();
			storeJson(redditJson, submissionsFileName);
		}
	)
}

function displaysubmissions(){
	if(noNewComments>0){
		var injectContentFunction = function(documentRef){
			documentRef.getElementById("noNewComments").innerHTML = noNewComments;
		}
		thisApplication.showNotification("new_comments.html",injectContentFunction)
	}
	var sortedArray = new Array();
	var submissionsHtml = "";
	submissionsHtml+=""+
					"<thead> "+
					"<tr> "+
					    "<th class='sort-alpha votes'>V</th> "+
						"<th class='sort-alpha upvotes'>U</th> "+
						"<th class='sort-alpha downvotes'>D</th> "+
						"<th class='sort-alpha newUpvotes'>NU</th> "+
						"<th class='sort-alpha newDownvotes'>ND</th> "+
						"<th class='sort-alpha comments'>C</th> "+
						"<th class='sort-alpha newComments'>NC</th> "+
					    "<th>Submission</th> "+
					"</tr> "+
					"</thead> "+
					"<tbody> ";
	for(var i = 0; i<newSubmissionsArray.length; i++){
		submissionsHtml+=""+
					"<tr> "+
					    "<td>"+(newSubmissionsArray[i].data.ups-newSubmissionsArray[i].data.downs)+"</td>"+
						"<td>"+newSubmissionsArray[i].data.ups+"</td>"+
						"<td>"+newSubmissionsArray[i].data.downs+"</td> "+
					    "<td class='newUpvotes'>"+newSubmissionsArray[i].data.newups+"</td>"+
						"<td class='newDownvotes'>"+newSubmissionsArray[i].data.newdowns+"</td> "+
						"<td class='noComments'>"+newSubmissionsArray[i].data.num_comments+"</td> "+
						"<td class='newComments'>"+newSubmissionsArray[i].data.newComments+"</td> "+
					    "<td class='text'><span class='subject'><a href='"+newSubmissionsArray[i].data.url+"'>"+newSubmissionsArray[i].data.title+"</a><span class='subreddit'> (in <a href='http://www.reddit.com/r/"+newSubmissionsArray[i].data.subreddit+"'>/r/"+newSubmissionsArray[i].data.subreddit+"</a>)</span></span></td> "+
					"</tr>";

	}
	submissionsHtml+="</tbody>";
	$('#theSubmissions table').html(submissionsHtml);
	sortables_init();
	thisApplication.nativeWin.dispatchEvent(loadingCompleteEvent);
	//tooltips for table heads
	$('#submissionsSortTab table.sortable th.votes').simpletip({
		content: 'Total Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#submissionsSortTab table.sortable th.upvotes').simpletip({
		content: 'Up Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#submissionsSortTab table.sortable th.downvotes').simpletip({
		content: 'Down Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#submissionsSortTab table.sortable th.newUpvotes').simpletip({
		content: 'New Up Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#submissionsSortTab table.sortable th.newDownvotes').simpletip({
		content: 'New Down Votes',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#submissionsSortTab table.sortable th.comments').simpletip({
		content: 'Total number of comments',
		fixed: true,
		position: ["0", "-30"]
	});
	$('#submissionsSortTab table.sortable th.newComments').simpletip({
		content: 'Number of new comments',
		fixed: true,
		position: ["0", "-30"]
	});
}

function processsubmissions(){
	var i = 0;
	var j = 0;
	newSubmissions.at = now.getTime();
	noNewComments = 0;
	while (!(i == oldSubmissionsArray.length || j == newSubmissionsArray.length)) {
		if (oldSubmissionsArray[i].data.id == newSubmissionsArray[j].data.id) {
			newSubmissionsArray[j].data.newups = (newSubmissionsArray[j].data.ups - oldSubmissionsArray[i].data.ups);
			newSubmissionsArray[j].data.newdowns = (newSubmissionsArray[j].data.downs - oldSubmissionsArray[i].data.downs);
			newSubmissionsArray[j].data.newComments = (newSubmissionsArray[j].data.num_comments - oldSubmissionsArray[i].data.num_comments);
			noNewComments+=newSubmissionsArray[j].data.newComments;
			i++;
			j++;
		}
		else 
			if (compareSubmissionIds(oldSubmissionsArray[i].data.id, newSubmissionsArray[j].data.id) == oldSubmissionsArray[i].data.id) {
				//when the user has deleted some submissions. So nothing to do here
				i++;
			}
			else {//when new submissionid greater than old submission id (i.e. when the user has made new submissions)
				newSubmissionsArray[j].data.newups = (newSubmissionsArray[j].data.ups);
				newSubmissionsArray[j].data.newdowns = (newSubmissionsArray[j].data.downs);
				newSubmissionsArray[j].data.newComments = newSubmissionsArray[j].data.num_comments;
				j++;
			}
	}
}

function compareSubmissionIds(submissionId1, submissionId2){
	//returns the submissionId that is the greatest
	length1 = submissionId1.length;
	length2 = submissionId2.length;
	if(submissionId1.charAt(length1-1)>submissionId2.charAt(length2-1)){
		return submissionId1;
	}
	else
		if(submissionId1.charAt(length1-1)<submissionId2.charAt(length2-1)){
			return submissionId2;
		}
		else{
			var biggerSubmissionId = compareSubmissionIds(submissionId1.substr(0,length1-1), submissionId2.substr(0,length2-1));
			if(biggerSubmissionId==-1) alert("Error");
			if(submissionId1.match(biggerSubmissionId)){
				return submissionId1
			}
			else if(submissionId2.match(biggerSubmissionId)){
				return submissionId2;
			}
			else return -1;
		}
}